package generic;

import java.util.*;

/**
 * 泛型语法和练习
 * interface 接口<T>{
 *
 * }
 *
 * class 类<K, V>{
 *
 * }
 *
 */
public class GenericExercise {

    public static void main(String[] args) {

        /*
            泛型的声明

            interface 接口<T>{

            }
            class 类<K,V>{

            }
            1)T K V不代表值，而是表示类型
            2)任意字母都可以，常用T表示，是Type缩写
         */

        /*
            泛型的实例化

            要在类名后面指定类型参数的值(类型)
            1)List<String> strList = new ArrayList<String>();
            2)Iterator<Customer> iterator = customers.iterator();
         */

        /*
            创建3个学生对象
            放入到HashSet中
            放入到HashMap中 key:name value:学生对象
            使用两种方式遍历
         */

        HashSet<Student> students = new HashSet<Student>();
        students.add(new Student("jack", 18));
        students.add(new Student("tom", 28));
        students.add(new Student("mary", 19));

        //遍历
        for (Student student : students) {
            System.out.println(student);
        }

        //使用泛型方式给HashMap放入3个学生对象
        HashMap<String, Student> hm = new HashMap<String, Student>();
        hm.put("milan", new Student("milan", 38));
        hm.put("smith", new Student("smith", 48));
        hm.put("hsp", new Student("hsp", 28));


        Set<Map.Entry<String, Student>> entries = hm.entrySet();
        Iterator<Map.Entry<String, Student>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Student> map = iterator.next();
            System.out.println(map.getKey() + "-" + map.getValue());
        }
    }
}

class Student {

    private String name;

    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
        ", age=" + age +
                '}';

    }
}